package com.syh.jn.scm.service.impl.supplier.finance.archive;

import cn.redscarf.commons.wrapper.Restrictions;
import cn.redscarf.commons.wrapper.Wrapper;
import cn.redscarf.commons.wrapper.expression.orderExpression.Order;
import com.alibaba.dubbo.config.annotation.Service;
import com.syh.core.ddd.infrastructure.repository.AggregateRepository;
import com.syh.core.ddd.service.AbstractServiceImpl;
import com.syh.jn.scm.domain.supplier.entity.finance.archive.Archive;
import com.syh.jn.scm.domain.supplier.repository.finance.archive.ArchiveReadRepository;
import com.syh.jn.scm.domain.supplier.repository.finance.archive.ArchiveRepository;
import com.syh.jn.scm.dto.supplier.finance.archive.ArchiveSto;
import com.syh.jn.scm.dto.supplier.finance.archive.ArchiveSettlementsVo;
import com.syh.jn.scm.dto.supplier.finance.archive.ArchiveStockVo;
import com.syh.jn.scm.dto.supplier.finance.archive.ArchiveVo;
import com.syh.jn.scm.service.api.supplier.finance.archive.ArchiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by soci.song on 2019/3/1.
 */
@Service
@Transactional
public class ArchiveServiceImpl extends AbstractServiceImpl<Object, Object, ArchiveVo, Archive, Long> implements ArchiveService {

  @Autowired
  private ArchiveRepository repo;

  @Resource(name = "archiveRepoMbImpl")
  private ArchiveReadRepository MbRepo;

  @Override
  protected AggregateRepository<Archive, Long> getAggregateRepository() {
    return null;
  }

  @Override
  public void archive(ArchiveSto sto) {
    Archive archive = new Archive(sto);
    this.save(archive);
  }

  private Long save(Archive entity) {
    return repo.add(entity);
  }

  @Override
  public void archiveCancel(Wrapper we) {
    List<Archive> list = this.repo.getList(we);
    list.forEach(archive->{
      archive = this.repo.getById(archive.getId());
      archive.cancel();
      this.repo.remove(archive);
    });
  }

  @Override
  public ArchiveVo getById(Long aLong) {
    return MbRepo.getById(aLong);
  }

  @Override
  public boolean contains(Long aLong) {
    return MbRepo.contains(aLong);
  }

  @Override
  public ArchiveVo getOne(Wrapper wrapper) {
    return MbRepo.getOne(wrapper);
  }

  @Override
  public List<ArchiveVo> findList(Wrapper wrapper) {
    return MbRepo.getList(wrapper);
  }

  @Override
  public List<ArchiveVo> findListPage(Wrapper wrapper, Integer startRow, Integer count) {
    return MbRepo.findListPage(wrapper, startRow, count);
  }

  @Override
  public int getCount(Wrapper wrapper) {
    return repo.getCount(wrapper);
  }

  @Override
  public int getCountView(Wrapper wrapper) {
    return MbRepo.getCount(wrapper);
  }

  @Override
  public List<ArchiveSettlementsVo> findSettlementsItemsPage(Wrapper we, Integer startRow, Integer count) {
    return (List<ArchiveSettlementsVo>) MbRepo.findSettlementsPage(we,startRow,count);
  }

  @Override
  public List<ArchiveStockVo> findStockItemsPage(Wrapper we, Integer startRow, Integer count) {
    return (List<ArchiveStockVo>) MbRepo.findStockPage(we,startRow,count);
  }

  @Override
  public Integer countSettlements(Wrapper we) {
    return MbRepo.countSettlements(we);
  }

  @Override
  public Integer countStock(Wrapper we) {
    return MbRepo.countStock(we);
  }
}
